package org.example.demo.module

import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.snapshots.SnapshotStateList
import cafe.adriel.voyager.core.model.ScreenModel
import cafe.adriel.voyager.core.model.screenModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.launch
import org.example.demo.bot.response.BotListResponse
import org.example.demo.bot.response.BotSimpleInfo
import org.example.demo.util.BOT_ID_SPACE
import org.example.demo.util.BOT_LIST
import org.example.demo.util.HEADER

class BotModule : ScreenModel {

    private val _bots = mutableStateListOf<BotSimpleInfo>()
    val bots: SnapshotStateList<BotSimpleInfo> = _bots

    init {
        requestBots()
    }

    private fun requestBots() {
        screenModelScope.launch(Dispatchers.IO) {
             val response = HttpService.get<BotListResponse>(
                BOT_LIST,
                HEADER,
                mapOf("workspace_id" to BOT_ID_SPACE)
            )
            response.data?.items?.let {
                _bots.addAll(it)
            }
        }
    }
}